package com.it.service.Impl;

import com.it.mapper.StudentMapper;
import com.it.pojo.Classes;
import com.it.pojo.Student;
import com.it.pojo.Team;
import com.it.service.CandidateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Slf4j
@Service
public class CandidateServiceImpl implements CandidateService {

    @Autowired
    private StudentMapper mapper;

    @Override
    public Student candidate(Integer classes, Integer team) {//选人
        List<Student> list = mapper.selectByConditionX(classes, team);
        if (list.size() == 0) {
            list = mapper.selectByCondition(classes, team);
        }
        Double weight = 0D;
        for (Student student : list) {
            Double number = 10000D;
            for (int i = 0; i < student.getNumber(); i++) {
                number /= 2;
            }
            weight += number;
        }
        Random r = new Random();
        Double num = r.nextDouble(weight);
        Double now = 0D;
        for (Student student : list) {
            Double number = 10000D;
            for (int i = 0; i < student.getNumber(); i++) {
                number /= 2;
            }
            now += number;
            if (num < now) {
                mapper.addNumber(student.getId());
                log.info(student.getName() + "+1");
                // student.setNumber(student.getNumber() + 1);
                return student;
            }
        }
        return null;
    }

}
